package com.zxshhy.controller;

import com.zxshhy.entity.Order;
import com.zxshhy.service.OrderService;
import com.zxshhy.vo.OrderVo;
import com.zxshhy.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author zxs
 */
@RefreshScope
@RestController
@RequestMapping("/xaat/order")
@Slf4j
public class OrderXAATController {

    @Value("${zxshhy.seata.data-source-proxy-mode:XXXX}")
    private String trxMode;
    @Resource
    private OrderService orderService;


    /**
     * 采用二阶段提交XA，AT强一致性实现分布式事务
     * @param orderVo
     * @return
     */
    @PostMapping("/createOrder")
    public ResultVo createOrder(@RequestBody OrderVo orderVo){
        log.info("收到使用{}模式下单请求,用户:{}, 商品编号:{}", trxMode,orderVo.getUserId(), orderVo.getCommodityCode());
        Order order = orderService.saveOrder(orderVo);
        return ResultVo.ok().put("order",order);
    }

}
